/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ifrr.gepro.sprint.handler;

import br.ifrr.gepro.estoria.bean.EstoriasBean;
import br.ifrr.gepro.estoria.dao.EstoriasDao;
import br.ifrr.gepro.pessoa.bean.TecnicoBean;
import br.ifrr.gepro.pessoa.dao.TecnicoDao;
import br.ifrr.gepro.sprint.bean.EstDoSprintBean;
import br.ifrr.gepro.sprint.bean.SprintBean;
import br.ifrr.gepro.sprint.dao.EstDosprintDao;
import br.ifrr.gepro.sprint.dao.SprintDao;
import br.ifrr.gepro.util.DAOException;
import br.ifrr.gepro.util.JsfUtil;
import br.ifrr.gepro.util.Navegacao;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

/**
 *
 * @author rafael
 */
@ManagedBean(name = "estsprint")
public class EstDoSprintMb {

    private EstDoSprintBean novaestsp = new EstDoSprintBean();
    private ArrayList<EstDoSprintBean> listaEsTSprint = new ArrayList<EstDoSprintBean>();
    private HtmlSelectOneMenu tecnicoSelecionado = new HtmlSelectOneMenu();
    private HtmlSelectOneMenu estoriaSelecionada = new HtmlSelectOneMenu();
    private HtmlSelectOneMenu sprintSelecionado = new HtmlSelectOneMenu();
    private EstoriasBean[] estorias;

    public String salvar() throws DAOException {
        novaestsp.setFkEstoria((EstoriasBean) new EstoriasDao().selecionar(Integer.parseInt(estoriaSelecionada.getValue().toString())));
        novaestsp.setFkResponsavel((TecnicoBean) new TecnicoDao().selecionar(Integer.parseInt(tecnicoSelecionado.getValue().toString())));
        novaestsp.setFkSprint((SprintBean) new SprintDao().selecionar(Integer.parseInt(sprintSelecionado.getValue().toString())));
        if (novaestsp.getId() == null) {
            new EstDosprintDao().inserir(getNovaestsp());
        } else {
            new EstDosprintDao().alterar(getNovaestsp());
        }
        JsfUtil.showMessageInfo("cadastro realizado com sucesso!");
        setNovaestsp(new EstDoSprintBean());
        return Navegacao.paginaPrincipal;
    }

    /**
     * @return the novaestsp
     */
    public EstDoSprintBean getNovaestsp() {
        return novaestsp;
    }

    public String salvarEstorias() throws DAOException {
        SprintBean sprint = (SprintBean) new SprintDao().selecionar((Integer.parseInt(JsfUtil.getValorDeParamentro("idSprint"))));
        sprint.setEstoriaBeanCollection(Arrays.asList(estorias));
        new SprintDao().alterar(sprint);
        JsfUtil.showMessageInfo("Cadastro Realizado com sucesso!");
        return Navegacao.paginaPrincipal;

    }

    /**
     * @param novaestsp the novaestsp to set
     */
    public void setNovaestsp(EstDoSprintBean novaestsp) {
        this.novaestsp = novaestsp;
    }

    public ArrayList<EstDoSprintBean> getListaEsTSprint() throws DAOException {
        listaEsTSprint = (ArrayList<EstDoSprintBean>) new EstDosprintDao().listar();
        return listaEsTSprint;
    }

    public void setListaEsTSprint(ArrayList<EstDoSprintBean> listaEsTSprint) {
        this.listaEsTSprint = listaEsTSprint;
    }

    public List<EstDoSprintBean> getEstSprints() throws DAOException {
        return new EstDosprintDao().listar();
    }

    public List<SelectItem> getEstSprintCombo() throws DAOException {
        List<SelectItem> lista = new ArrayList<SelectItem>();
        for (EstDoSprintBean e : getEstSprints()) {
            //  lista.add(new SelectItem(e.getId()), e.getTipo());
        }
        return lista;
    }

    public EstoriasBean[] getEstorias() {
        return estorias;
    }

    public void setEstorias(EstoriasBean[] estorias) {
        this.estorias = estorias;
    }
}
